#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <types.h>

typedef struct file {
    u8 *file_addr;
    u32 file_ptr_pos;
} file_t;

inline file_t *fopen_nos(const char *file, const char *mode)
{
    file_t *fd = (file_t *)malloc(sizeof(file_t));
    fd->file_addr = (u8 *)file;
    fd->file_ptr_pos = 0;
    return fd;
}

inline int fclose_nos(file_t *fd)
{
    free(fd);
    fd = 0;
    return 0;
}

u8 fgetc_nos(file_t *fd)
{
    if(!fd) return 0;
    ++fd->file_ptr_pos;
    return fd->file_addr[fd->file_ptr_pos - 1];
}

int fread_nos(void *dst, u32 len, u32 num, file_t *fd)
{
    if(!fd || len == 0 || num == 0) return 0;
    memcpy(dst, &fd->file_addr[fd->file_ptr_pos], len * num);
    fd->file_ptr_pos += len * num;
    return len * num;
}

inline int fwrite_nos(void *src, u32 len, u32 num, file_t *fd)
{
    if(!fd || len == 0 || num == 0) return 0;
    memcpy(&fd->file_addr[fd->file_ptr_pos], src, len * num);
    fd->file_ptr_pos += len * num;
    return len * num;
}

